/*
**  Copyright (C), CHENGDU GONGWEI TECHNOLOGY CO.,LTD.
**
**  Project:	Manager Unit v1.0
**  File:    	
**  Author: 	zhaozhuyu
**  Date:    	2017/05/20
**
**  Purpose:
**    		.
**
**  History: 
**  <author>   <time>          <version >   <desc>
*	$$Author$$
*/


#ifndef __SUBSYSTEMCORE_H__
#define __SUBSYSTEMCORE_H__


/* Include files. */
#include "cplusplus.h"

/* Macro constant definitions. */

/* Type definitions. */

/* External variable declarations. */

/* External function declarations. */

/* Macro API definitions. */

class netObject;
class event;
class message;
class timeObj;
class subSystem 
{
public:
	subSystem(string name, lua_State *L);
	virtual ~subSystem();

	timeObj* findTimeObj(u32 key);
	void removeTimeObj(u32 key);
	netObject* findNetObject(u32 key); 
	void addNetObject(netObject* netobj); 
	void removeNetObject(netObject* netobj); 
	
	static void createSubSystem(subSystem *sub);
	static void deleteSubSystem(subSystem *sub);
	static subSystem* findSubSystem(string key);
	static bool bindSubSystem(string name, netObject *netobj);
	static map<string, subSystem *> sub_system_manager;

	u32 messageSize() 
	{
		return _msg_queue.size();
	}
	static queue<subSystem *> message_queue;

	void addEvent(event *ev);
	void removeEvent(event *ev);

	int sendSubSystem(lua_State *L);
	int sendSubSystem(lua_State *L, 
				const char *to, const char *type, 
				const char *msg, u32 session); 
	int sendNetObject(lua_State *L); 
	
public:
	string _name;
	queue<message*> _msg_queue;	
	map<u32, timeObj*> _time_obj_map;
	set<event*> _event_set;
	map<u32, netObject*> _net_object_map;
	
	/*status*/
	lua_State *_L;
	u32 _net_object_count;
	u32 _net_object_id;
	u32 _cost_mem;
	u64 _cost_cpu;
	u32 _dispatch_count;
};

int luaNewSubSystem(lua_State *L);
int sendSubSystem(lua_State *L, const char *from, 
		const char *to, const char *type, const char *msg, 
		u32 session); 

#endif

